package com.example.xiaomi.Model.Service;

import com.example.xiaomi.Model.Controller.DaoList;
import com.example.xiaomi.Model.Controller.adddetailsList;
import com.example.xiaomi.Model.entity.UeserEntity;
import com.example.xiaomi.Model.entity.adddetailsEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/addcart")
public class addcart extends HttpServlet {
    adddetailsList adddetailsList = new adddetailsList();
    DaoList dao = new DaoList();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getParameter("arr");
        String[] arr = data.split(",");
        if (arr.length == 4){
            int id = Integer.parseInt(arr[0]);
            int commodityid = Integer.parseInt(arr[1]);
            int price = Integer.parseInt(arr[2]);
            String mail = arr[3];
            PrintWriter out = resp.getWriter();
            out.println(arr[3]+","+id);
            List<adddetailsEntity> list = adddetailsList.selectById(id);
            int conid = Integer.parseInt(arr[1]);
            int userid = Integer.parseInt(arr[0]);
            for (adddetailsEntity entity :list) {
                if (entity.getCid() == conid){
                    BigDecimal multiply = entity.getTotalcost().multiply(BigDecimal.valueOf(2));
                    adddetailsList.doUpdate(userid,conid,arr[3],entity.getDamount()+1,multiply,userid);
                    break;
                }else if (entity.getCid() != conid){
                    adddetailsList.doInsert(id,commodityid,mail,1, BigDecimal.valueOf(price));
                    break;
                }
            }
            String compared = String.valueOf(list);
            if (compared.equals("[]")){
                adddetailsList.doInsert(id,commodityid,mail,1, BigDecimal.valueOf(price));
            }
        }else if (arr.length == 3){
            int id = Integer.parseInt(arr[0]);
            int productid = Integer.parseInt(arr[1]);
            BigDecimal price = new BigDecimal(arr[2]);

            UeserEntity entity = dao.selectById(id);
            List<adddetailsEntity> list = adddetailsList.selectById(id);
            for (adddetailsEntity adddetailsEntity :list) {
                System.out.println(adddetailsEntity.getCid());
                System.out.println(productid);
                if (adddetailsEntity.getCid() == productid) {
                    adddetailsList.doUpdate(id,productid,entity.getUpostbox(),adddetailsEntity.getDamount()+1,price,id);
                    break;
                }else {
                    adddetailsList.doInsert(id,productid,entity.getUpostbox(),1,price);
                    break;
                }
            }
            String compared = String.valueOf(list);
            if (compared.equals("[]")){
                adddetailsList.doInsert(id,productid,entity.getUpostbox(),1,price);
            }
        }

    }
}
